import { ImageResponse } from 'next/og'; import { t } from '@/i18n'; import { api, safe } from '@/lib/api'; import { displayValue } from '@/lib/format'; import { OG_INK2, OG_INK3, OG_RULE, OG_SIZE_H, OG_SIZE_W, OgFrame, OgWordmark } from '../../og-shared'; export const alt = 'Country statistics on CountryAtlas'; export const size = { width: OG_SIZE_W, height: OG_SIZE_H }; export const contentType = 'image/png'; export const revalidate = 3600; /** Per-country social image: flag + name + three headline values (population, GDP per capita, life expectancy). */ export default async function CountryOgImage({ params }: { params: Promise<{ slug: string }> }) { const { slug } = await params; const data = await safe(api.country(slug)); const c = data?.country; const name = c?.name ?? slug.replace(/-/g, ' '); const pick = (id: string) => data?.headline.find((m) => m.indicator === id && m.has_data) ?? null; const facts = [pick('population'), pick('gdp-per-capita'), pick('life-expectancy')].filter((m): m is NonNullable => !!m).slice(0, 3); const sub = [c?.capital, c?.region_name, c?.income_name].filter(Boolean).join(' ยท '); return new ImageResponse( (
{c?.flag ?? ''}
{name}
{sub ?
{sub}
: null}
{facts.length === 0 ? (
{t('country.description', { name }).split(':')[1]?.trim() ?? ''}
) : null} {facts.map((m) => (
{m.indicator_name ?? m.indicator}
{displayValue(m.value, m, m.formatted)}
{String(m.year ?? '')}
))}
{t('og.site')}
), { ...size }, ); }